package com.earnings.okhttputils.utils.car.ui;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.earnings.R;
import com.earnings.okhttputils.utils.OkHttp.OkHttpUtils;
import com.earnings.okhttputils.utils.UrlConstant.CarUrl;
import com.earnings.okhttputils.utils.bean.BrandCar2Data;
import com.earnings.okhttputils.utils.bean.BrandCar2ZData;
import com.earnings.okhttputils.utils.bean.BrandCarData;
import com.earnings.okhttputils.utils.car.http.CarHttp;
import com.earnings.okhttputils.utils.car.http.GodHttpArrayCallback;
import com.earnings.okhttputils.utils.god.GodAdapter.GodArrayList;
import com.earnings.okhttputils.utils.god.GodAdapter.GodBaseAdapter;
import com.earnings.okhttputils.utils.god.GodAdapter.GodViewHolder;
import com.earnings.okhttputils.utils.god.GodLeftHandBaseActivity;
import com.earnings.okhttputils.utils.https.HttpArrayCallback;
import com.gjiazhe.wavesidebar.WaveSideBar;

import java.util.List;

/**
 * 品牌找车
 * Created by Administrator on 2017/6/16 0016.
 */

public class BrandCarActivity extends GodLeftHandBaseActivity {
    private GodArrayList list;
    private GodArrayList<BrandCar2Data> bottomlist;
    private WaveSideBar waveSideBar;
    private RecyclerView rv_content;

    @Override
    protected int onCreateLayoutId() {
        return R.layout.activity_brandcar;
    }

    @Override
    protected void init() {
        setTitle("品牌找车");
        list = new GodArrayList();
        bottomlist = new GodArrayList();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        setRecyclerViewLayoutManager(R.id.recyclerview, linearLayoutManager).
        setAdapter(new GodBaseAdapter<BrandCarData>(R.layout.item_brandcar, list) {
            @Override
            public void onBind(GodViewHolder holder, int position, BrandCarData data) {
                Glide.with(BrandCarActivity.this).load(data.getLogo()).into((ImageView) holder.getView(R.id.img_view));
                holder.setText(R.id.name_tv, data.getName());
            }

            @Override
            public void OnItemClickListener(View view, int position, BrandCarData data) {
                mIntent.putExtra("title", data.getName());
                mIntent.putExtra("id", data.getId());
                skipActivity(CarListActivity.class);
            }
        });
        CarHttp http = new CarHttp("brand_passed");
        OkHttpUtils.get().url(CarUrl.CAR_URL).params(http).build().execute(new GodHttpArrayCallback<BrandCarData>() {
            @Override
            public void onSuccess(List<BrandCarData> datas, String msg, String response) {
                list.addAll(datas);
            }

            @Override
            public void onFail(int code, String msg) {

            }
        });
        //下面
        waveSideBar = getView(R.id.side_bar);
        rv_content = getView(R.id.rv_content);
        rv_content.setLayoutManager(new LinearLayoutManager(this));
        rv_content.setAdapter(new GodBaseAdapter<BrandCar2Data>(R.layout.item_brandcar2, bottomlist) {
            @Override
            public void onBind(GodViewHolder holder, int position, BrandCar2Data contact) {
                if (position == 0 || !bottomlist.get(position - 1).getIndex().equals(contact.getIndex())) {
                    holder.setVisibility(R.id.tv_index, View.VISIBLE);
                    holder.setText(R.id.tv_index, contact.getIndex());
                } else {
                    holder.setVisibility(R.id.tv_index, View.GONE);
                }

                holder.setText(R.id.tv_index, contact.getTitle());
                GodArrayList<BrandCar2ZData> zdatas = new GodArrayList();
                zdatas.addAll(contact.getItems());
                RecyclerView recy = holder.getView(R.id.recyclerview);
                LinearLayoutManager lin = new LinearLayoutManager(BrandCarActivity.this) {
                    @Override
                    public boolean canScrollVertically() {
                        return false;
                    }
                };
                recy.setLayoutManager(lin);
                recy.setAdapter(new GodBaseAdapter<BrandCar2ZData>(R.layout.item_zbrandcar2, zdatas) {
                    @Override
                    public void onBind(GodViewHolder holder, int position, BrandCar2ZData data) {
                        holder.setText(R.id.name_tv, data.getName());
                        Glide.with(BrandCarActivity.this).load(data.getLogo()).into((ImageView) holder.getView(R.id.img_view));
                    }

                    @Override
                    public void OnItemClickListener(View view, int position, BrandCar2ZData data) {
                        mIntent.putExtra("title", data.getName());
                        mIntent.putExtra("id", data.getId());
                        skipActivity(CarListActivity.class);
                    }
                });

            }
        });

        waveSideBar.setOnSelectIndexItemListener(new WaveSideBar.OnSelectIndexItemListener() {
            @Override
            public void onSelectIndexItem(String index) {
                for (int i = 0; i < bottomlist.size(); i++) {
                    if (bottomlist.get(i).getIndex().equals(index)) {
                        ((LinearLayoutManager) rv_content.getLayoutManager()).scrollToPositionWithOffset(i, 0);
                        return;
                    }
                }
            }
        });

        CarHttp http2 = new CarHttp("brand");
        showProgress();
        OkHttpUtils.get().url(CarUrl.CAR_URL).params(http2).build().execute(new GodHttpArrayCallback<BrandCar2Data>() {
            @Override
            public void onSuccess(List<BrandCar2Data> datas, String msg, String response) {
                for (int i = 0; i < datas.size(); i++) {
                    BrandCar2Data data = datas.get(i);
                    data.setIndex(data.getTitle());
                }
                bottomlist.addAll(datas);
                dismissProgress();
            }

            @Override
            public void onFail(int code, String msg) {
                dismissProgress();
            }
        });

    }


}
